package com.usian.controller;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.usian.common.dto.MemberTestHistoryDTO;
import com.usian.common.model.competition.CompetitionHistory;
import com.usian.common.model.member.MemberTestHistory;
import com.usian.common.model.paper.PaperHistory;
import com.usian.common.untils.Result;
import com.usian.common.vo.QuestionsVO;
import com.usian.service.MemberTestHistoryService;
import com.usian.service.PaperHistoryService;
import com.usian.service.QuestionsFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @projectName: 2210A-project1
 * @package: com.usian.controller
 * @className: PaperController
 * @author: 莫洪维
 * @email: 1204315800@qq.com
 * @description: TODO
 * @date: 2023/9/4 16:02
 */
@RestController
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    private PaperHistoryService paperHistoryService;
    @Autowired
    private MemberTestHistoryService memberTestHistoryService;
    @Autowired
    private QuestionsFeignService questionsFeignService;

    @RequestMapping(value = "/findQuestion",method = RequestMethod.GET)
    public Result findAllQuestition(){
        //调用方法进行查询
        List<QuestionsVO> list = questionsFeignService.findAll();
        return Result.ok().message("查询成功").data("list",list);
    }

    /**
     * 查询当前登录人的历史测验数量
     */
    @RequestMapping(value = "/findByMemberId",method = RequestMethod.GET)
    public Result findByMemberId(Long memberId){
        //调用方法进行查询
        QueryWrapper<PaperHistory> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PaperHistory::getMemberId,memberId);
        int num = paperHistoryService.count(wrapper);
        return Result.ok().message("查询成功").data("historyNum",num);
    }

    /**
     * 根据成员id查询历史测验
     */
    @RequestMapping(value = "/findByMemberHistory",method = RequestMethod.GET)
    public Result findByMemberHistory(MemberTestHistoryDTO memberTestHistoryDTO){
        //调用方法进行查询
        PageInfo<MemberTestHistory> pageInfo = memberTestHistoryService.findByMemberId(memberTestHistoryDTO);
        return Result.ok().message("查询成功").data("historyPageInfo",pageInfo);
    }

    /**
     * 根据成员id查询历史测验
     */
    @RequestMapping(value = "/findByMemberHistoryNum",method = RequestMethod.GET)
    public Result findByMemberHistoryNum(MemberTestHistoryDTO memberTestHistoryDTO){
        //调用方法进行查询
        PageInfo<MemberTestHistory> pageInfo = memberTestHistoryService.findByMemberId(memberTestHistoryDTO);
        return Result.ok().message("查询成功").data("historyNum",pageInfo.getList().size());
    }

    /**
     * 根据Id查询历史测验方法
     * @param id
     * @return
     */
    @RequestMapping(value = "/findMemberTestHistoryById",method = RequestMethod.GET)
    public Result findMemberTestHistoryById(Long id){
        //调用方法进行查询
        MemberTestHistory memberTestHistory = memberTestHistoryService.getById(id);
        return Result.ok().message("查询成功").data("memberTestHistory",memberTestHistory);
    }

    /**
     * 保存历史测验记录方法
     * @param memberTestHistory
     * @return
     */
    @RequestMapping(value = "/saveMemberTestHistory",method = RequestMethod.POST)
    public Result saveMemberTestHistory(@RequestBody MemberTestHistory memberTestHistory){
        //调用方法进行添加
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        memberTestHistory.setTestDateTime(simpleDateFormat.format(new Date()));
        boolean b = memberTestHistoryService.save(memberTestHistory);
        if (b){
            //保存此次做题记录
            QueryWrapper<PaperHistory> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(PaperHistory::getMemberTestHistoryId,0);
            PaperHistory paperHistory = new PaperHistory();
            paperHistory.setMemberTestHistoryId(memberTestHistory.getId());
            paperHistoryService.update(paperHistory,wrapper);
            return Result.ok().message("提交成功").data("id",memberTestHistory.getId());
        }else {
            return Result.error().message("提交失败");
        }
    }

    /**
     * 删除历史记录方法
     */
    @RequestMapping(value = "/delHistory",method = RequestMethod.DELETE)
    public Result delHistory(PaperHistory paperHistory){
        //调用方法进行删除
        QueryWrapper<PaperHistory> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(PaperHistory::getMemberTestHistoryId,paperHistory.getMemberTestHistoryId())
                .eq(PaperHistory::getMemberId,paperHistory.getMemberId());
        paperHistoryService.remove(wrapper);
        return Result.ok().message("删除成功");
    }

    /**
     * 查询历史测验方法
     * @param paperHistory
     * @return
     */
    @RequestMapping(value = "/toHistory",method = RequestMethod.GET)
    public Result toHistory(PaperHistory paperHistory){
        //调用方法进行查询
        List<PaperHistory> list = paperHistoryService.toHistory(paperHistory);
        return Result.ok().message("查询成功").data("paperHistory",list);
    }

    /**
     * 保存历史记录方法
     */
    @RequestMapping(value = "/saveHistory",method = RequestMethod.POST)
    public Result saveHistory(@RequestBody PaperHistory paperHistory){
        //判断是否有答案
        if (StringUtils.isBlank(paperHistory.getAnswer())){
            return Result.error();
        }
        //调用方法进行查询
        QueryWrapper<PaperHistory> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(PaperHistory::getPaperId,paperHistory.getPaperId());
        wrapper.lambda().eq(PaperHistory::getMemberId,paperHistory.getMemberId());
        wrapper.lambda().eq(PaperHistory::getQuestionsId,paperHistory.getQuestionsId());
        wrapper.lambda().eq(PaperHistory::getMemberTestHistoryId,paperHistory.getMemberTestHistoryId());
        PaperHistory one = paperHistoryService.getOne(wrapper);
        boolean b = false;
        if (one == null){
            b = paperHistoryService.save(paperHistory);
        }else {
            b = paperHistoryService.update(paperHistory,wrapper);
        }
        if (b){
            return Result.ok();
        }else {
            return Result.error();
        }
    }
}
